package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.project.bean.*;
import com.lovo.project.mapper.SaleInGoodsMapper;
import com.lovo.project.mapper.SaleInMapper;
import com.lovo.project.mapper.SaleMapper;
import com.lovo.project.mapper.WarehouseMapper;
import com.lovo.project.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/sale")
public class SaleController {
    @Autowired
    SaleService saleService;
    @Autowired
    WarehouseMapper warehouseMapper;
    @Autowired
    SaleMapper saleMapper;
    @Autowired
    SaleInMapper saleInMapper;
    @Autowired
    SaleInGoodsMapper saleInGoodsMapper;

    @RequestMapping("/querySaleList")
    public List<SaleBean> querySaleList(Integer userId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<SaleBean> list=saleService.querySaleList(queryWrapper);
        return list;
    }

    @RequestMapping("/deleteSale")
    public String deleteSale(Integer saleId){
        saleService.deleteSale(saleId);
        return "ok";
    }

    @RequestMapping("/addSale")
    public String addSale(Integer goodsId, Integer userId){
      WarehouseBean warehouseBean= warehouseMapper.selectById(goodsId);
        SaleBean saleBean=new SaleBean();
        if (saleMapper.selectById(goodsId)!=null){
            return "商品已有无法添加";
        }else {
            saleBean.setSaleId(warehouseBean.getGoodsId());
            saleBean.setSaleGoodsName(warehouseBean.getGoodsName());
            saleBean.setSaleBrand(warehouseBean.getGoodsBrand());
            saleBean.setSalePrice(warehouseBean.getGoodsSalePrice());
            saleBean.setUserId(userId);
            saleService.insertSale(saleBean);
        }
        return "ok";
    }

    @RequestMapping("/updateSale")
    public String updateSale(Integer saleId,Integer saleNumber){
       WarehouseBean warehouseBean=warehouseMapper.selectById(saleId);
        SaleBean saleBean=new SaleBean();
       if (warehouseBean.getGoodsNumber()>= saleNumber){
        SaleBean saleBean1=saleMapper.selectById(saleId);
        saleBean.setSaleId(saleId);
        saleBean.setSalePrice(saleBean1.getSalePrice());
        saleBean.setSaleNumber(saleNumber);
        saleService.updateSale(saleBean);
        return "ok";
       }else{
           SaleBean saleBean1=saleMapper.selectById(saleId);
           saleBean.setSaleId(saleId);
           saleBean.setSalePrice(saleBean1.getSalePrice());
           saleBean.setSaleNumber(warehouseBean.getGoodsNumber());
           saleService.updateSale(saleBean);
       }
       return "fail";
    }

    @RequestMapping("/saleIssue")
    public String saleIssue(Integer userId,String userName, String clientName,double totalPrice2, String shippingAddress){
        SaleInBean saleInBean=new SaleInBean();
        saleInBean.setSaleInClientName(clientName);
        saleInBean.setSaleInSalesperson(userName);
        saleInBean.setSaleInTotalPrice(totalPrice2);
        saleInBean.setSaleInTime(LocalDate.now());
        saleInBean.setSaleInAddress(shippingAddress);
        saleInBean.setUserId(userId);
        saleInMapper.insert(saleInBean);
        Integer id = saleInBean.getSaleInId();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<SaleBean> list=saleMapper.selectList(queryWrapper);

        for (SaleBean b:list){
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("goods_id",b.getSaleId());
            WarehouseBean warehouseBean=warehouseMapper.selectOne(queryWrapper1);
            if (warehouseBean.getGoodsNumber()>=b.getSaleNumber()){
                warehouseBean.setGoodsNumber(warehouseBean.getGoodsNumber()-b.getSaleNumber());
                warehouseMapper.updateById(warehouseBean);
            }
            SaleInGoodsBean saleInGoodsBean=new SaleInGoodsBean();
            saleInGoodsBean.setSaleingoodsId(b.getSaleId());
            saleInGoodsBean.setSaleInGoodsName(b.getSaleGoodsName());
            saleInGoodsBean.setSaleInGoodsBrand(b.getSaleBrand());
            saleInGoodsBean.setSaleInGoodsPrice(b.getSalePrice());
            saleInGoodsBean.setSaleInGoodsNumber(b.getSaleNumber());
            saleInGoodsBean.setSaleInId(id);
            saleInGoodsMapper.insert(saleInGoodsBean);
        }
        QueryWrapper<SaleBean> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", userId);
        saleMapper.delete(queryWrapper2);
        return "ok";
    }
}
